package com.kangdi.server.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.kangdi.server.mapper.EmployeeMapper;
import com.kangdi.server.pojo.Employee;
import com.kangdi.server.pojo.RespBean;
import com.kangdi.server.pojo.RespPageBean;
import com.kangdi.server.service.IEmployeeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.mail.MailProperties;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.stereotype.Service;
import org.thymeleaf.TemplateEngine;
import org.thymeleaf.context.Context;

import javax.mail.MessagingException;
import javax.mail.internet.MimeMessage;
import java.text.DecimalFormat;
import java.time.LocalDate;
import java.time.temporal.ChronoUnit;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * InnoDB free: 7168 kB; (`departmentId`) REFER `yeb/t_department`(`id`); (`jobLeve 服务实现类
 * </p>
 *
 * @author kangdi
 * @since 2022-10-04
 */
@Service
public class EmployeeServiceImpl extends ServiceImpl<EmployeeMapper, Employee> implements IEmployeeService {
    @Autowired
    private EmployeeMapper employeeMapper;
    @Autowired
    private JavaMailSender javaMailSender;
    @Autowired
    private MailProperties mailProperties;
    @Autowired
    private TemplateEngine templateEngine;
    /**
     * 查询员工信息(分页)
     * @param currentPage
     * @param size
     * @param employee
     * @param beginDateScope
     * @return
     */
    @Override
    public RespPageBean getEmployeeByPage(Integer currentPage, Integer size, Employee employee, LocalDate[] beginDateScope) {
        Page<Employee> employeePage = new Page<>(currentPage,size);
        IPage<Employee> employeeByPage = employeeMapper.getEmployeeByPage(employeePage, employee, beginDateScope);
        RespPageBean respPageBean = new RespPageBean(employeeByPage.getTotal(),employeeByPage.getRecords());
        return respPageBean;
    }

    /**
     * 获取最大员工号
     * @return
     */
    @Override
    public RespBean getMaxWorkID() {
        List<Map<String, Object>> maps = employeeMapper.selectMaps(new QueryWrapper<Employee>().select("max(WorkID)"));
        return  RespBean.success(null,String.format("%08d",Integer.parseInt(maps.get(0).get("max(WorkID)").toString())+1));
    }

    /**
     * 添加员工
     * @param employee
     * @return
     */
    @Override
    public RespBean addEmployee(Employee employee) {
        LocalDate beginContract = employee.getBeginContract();
        LocalDate endContract = employee.getEndContract();
        DecimalFormat decimalFormat=new DecimalFormat("##.00");
        long day = beginContract.until(endContract, ChronoUnit.DAYS);
        employee.setContractTerm(Double.parseDouble(decimalFormat.format(day/365)));
        if (1==employeeMapper.insert(employee)){
            Employee employee1 = employeeMapper.getEmployee(employee.getId()).get(0);
            MimeMessage mimeMessage = javaMailSender.createMimeMessage();
            MimeMessageHelper mimeMessageHelper = new MimeMessageHelper(mimeMessage);
            try {
                mimeMessageHelper.setFrom(mailProperties.getUsername());
                mimeMessageHelper.setTo(employee.getEmail());
                mimeMessageHelper.setSubject("入职信息");
                mimeMessageHelper.setSentDate(new Date());
                Context context = new Context();
                context.setVariable("name",employee1.getName());
                context.setVariable("posName",employee1.getPosition().getName());
                context.setVariable("joblevelName",employee1.getJoblevel().getName());
                context.setVariable("departmentName",employee1.getDepartment().getName());
                String mail = templateEngine.process("mail", context);
                mimeMessageHelper.setText(mail,true);
                javaMailSender.send(mimeMessage);
                return RespBean.success("添加成功");
            } catch (MessagingException e) {
                e.printStackTrace();
            }
        }
        return RespBean.error("添加失败");
    }

    /**
     * 员工信息下载
     * @param id
     * @return
     */
    @Override
    public List<Employee> getEmployee(Integer id) {
        return employeeMapper.getEmployee(id);
    }

    /**
     * 查询所有员工的工资账套
     * @param currentPage
     * @param size
     * @return
     */
    @Override
    public RespPageBean getEmployeeWithSalary(Integer currentPage, Integer size) {
        Page<Employee> employeePage = new Page<>(currentPage, size);
        IPage<Employee> employeeIPage= employeeMapper.getEmployeeWithSalary(employeePage);
        RespPageBean respPageBean = new RespPageBean(employeeIPage.getTotal(), employeeIPage.getRecords());
        return respPageBean;
    }

}
